package com.app.framework.web.service;/**
 * @program: app-admin
 * @name AppLoginService
 * @description: App用户登录
 */

import com.app.common.constant.Constants;
import com.app.common.core.domain.model.AppLoginUser;
import com.app.common.core.domain.model.AppRegisterBody;
import com.app.common.enums.DeleteEnum;
import com.app.common.exception.ServiceException;
import com.app.common.utils.MessageUtils;
import com.app.common.utils.SecurityUtils;
import com.app.common.utils.StringUtils;
import com.app.framework.manager.AsyncManager;
import com.app.framework.manager.factory.AsyncFactory;
import com.app.system.domain.AppUser;
import com.app.system.service.IAppUserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 *@author:
 *@program: app-admin
 *@name AppLoginService
 *@description: App用户登录
 */
@Slf4j
@Component
public class AppLoginService {

    @Autowired
    private AppTokenService appTokenService;

    @Autowired
    private IAppUserService appUserService;


    /**
     * @param apUrPhone 用户名
     * @param apUrPwd 密码
     * @param code 验证码
     * @param uuid 唯一标识
     * @return {@link String}
     */
    public String appLogin(String apUrPhone, String apUrPwd, String code, String uuid) {
        if (StringUtils.isEmpty(apUrPhone)) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor("APP用户", Constants.LOGIN_FAIL, MessageUtils.message("app.user.name.is.null")));
        }
        if (StringUtils.isEmpty(apUrPwd)) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor("APP用户", Constants.LOGIN_FAIL, MessageUtils.message("app.user.pwd.is.null")));
        }

        AppUser appUser = appUserService.getOne(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getApUrPhone, apUrPhone)
                .eq(AppUser::getDelFlag, DeleteEnum.NO.getCode()));
        if (StringUtils.isNull(appUser)) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor("APP用户", Constants.LOGIN_FAIL, MessageUtils.message("app.user.is.null")));
        }
        //判断密码是否相同
        boolean pwdFlag = SecurityUtils.matchesPassword(apUrPwd, appUser.getApUrPwd());
        if (!pwdFlag) {
            AsyncManager.me().execute(AsyncFactory.recordLogininfor("APP用户", Constants.LOGIN_FAIL, MessageUtils.message("app.user.pwd.is.error")));
        }

        AsyncManager.me().execute(AsyncFactory.recordLogininfor("APP用户", Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));
        recordAppLoginInfo(appUser);
        AppLoginUser appLoginUser = new AppLoginUser();
        BeanUtils.copyProperties(appUser, appLoginUser);
        return appTokenService.createToken(appLoginUser);
    }


    /**
     * App用户注册
     * @param appRegisterBody
     * @return {@link int}
     */
    public int appRegister(AppRegisterBody appRegisterBody) {
        long registerCount = appUserService.count(new LambdaQueryWrapper<AppUser>()
                .eq(AppUser::getApUrPhone, appRegisterBody.getApUrPhone())
                .eq(AppUser::getDelFlag, DeleteEnum.NO.getCode()));
        if (registerCount > 0) {
            throw new ServiceException("该账号已存在,不能重复注册");
        }
        AppUser appUser = new AppUser();
        appUser.setApUrPhone(appRegisterBody.getApUrPhone());
        appUser.setApUrPwd(SecurityUtils.encryptPassword(appRegisterBody.getApUrPwd()));
        int insertUser = appUserService.insertAppUser(appUser);
        return insertUser;
    }

    /**
     * 记录登录信息
     * @param appUser
     */
    public void recordAppLoginInfo(AppUser appUser) {
        AppUser user = new AppUser();
        user.setApUrId(appUser.getApUrId());
        user.setApUrLastTime(new Date());
        appUserService.updateById(user);
    }
}
